class GeneratorsController < ApplicationController
  # GET /generators
  # GET /generators.xml
  def index
    @generators = Generator.find(:all)

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @generators }
    end
  end

  # GET /generators/1
  # GET /generators/1.xml
  def show
    @generator = Generator.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @generator }
    end
  end

  # GET /generators;search
  # http://0.0.0.0:3000/generators/current_generation.xml/?q=192.168.1.101
  def current_generation
    logger.info "search ip " + params[:q]
   
    generator = Generator.find_by_ip_address(params[:q])
    #FIXME: this can be n
    @current_generation = Generation.find_by_generator_id(generator.id, :include => [:project])

    logger.info "search ip " + params[:q]

    respond_to do |format|
      format.html # show.html.erb
      format.xml { render :xml => @current_generation.to_xml(:include => :project) }
    end
  end

  # GET /generators/new
  # GET /generators/new.xml
  def new
    @generator = Generator.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @generator }
    end
  end

  # GET /generators/1/edit
  def edit
    @generator = Generator.find(params[:id])
  end

  # POST /generators
  # POST /generators.xml
  def create
    @generator = Generator.new(params[:generator])

    respond_to do |format|
      if @generator.save
        flash[:notice] = 'Generator was successfully created.'
        format.html { redirect_to(@generator) }
        format.xml  { render :xml => @generator, :status => :created, :location => @generator }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @generator.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /generators/1
  # PUT /generators/1.xml
  def update
    @generator = Generator.find(params[:id])

    respond_to do |format|
      if @generator.update_attributes(params[:generator])
        flash[:notice] = 'Generator was successfully updated.'
        format.html { redirect_to(@generator) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @generator.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /generators/1
  # DELETE /generators/1.xml
  def destroy
    @generator = Generator.find(params[:id])
    @generator.destroy

    respond_to do |format|
      format.html { redirect_to(generators_url) }
      format.xml  { head :ok }
    end
  end
end
